/**
 * 
 */
package com.hpxx.ylzswl.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hpxx.ylzswl.R;
import com.hpxx.ylzswl.base.BaseActivity;
import com.hpxx.ylzswl.https.HttpAddress;
import com.hpxx.ylzswl.https.RequestParams;
import com.universal.frame.generalutils.Constants;
import com.universal.frame.generalutils.JsonUtil;
import com.universal.frame.generalutils.ToastUtil;
import com.universal.frame.generalutils.timer.TimerCount;
import com.universal.frame.okhttp.OKHttpUtils;
import com.universal.frame.okhttp.ResultCallBack;

import java.io.IOException;

import okhttp3.Request;

/**
 * @ClassName RegisterActivity
 * @TitleName RegisterActivity
 * @author LuoXiang
 * @Data 2017-6-11 下午11:27:44
 */
public class FindPwdActivity extends BaseActivity implements ResultCallBack,View.OnClickListener{

	private TextView mTextViewCode;
	private EditText mEditTextPhone; //电话
	private EditText mEditTextCode; //验证码
	private EditText mEditTextPwd; //密码
	private EditText mEditTextComPwd; //密码
	private Button btn_regist;
	private String phone;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_findpwd);
		back();
		setTitle("找回密码");
	}

	@Override
	public void initView() {

		mTextViewCode = (TextView) findViewById(R.id.regist_cm_code);
		mEditTextPhone = (EditText) findViewById(R.id.et_regist_phone);
		mEditTextCode = (EditText) findViewById(R.id.et_input_code);
		mEditTextPwd = (EditText) findViewById(R.id.et_input_pwd);
		mEditTextComPwd = (EditText) findViewById(R.id.et_register_com_pwd);
		btn_regist = (Button) findViewById(R.id.btn_regist);

		mTextViewCode.setOnClickListener(this);
		btn_regist.setOnClickListener(this);

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {

			case R.id.regist_cm_code:
				getCodePost();
				break;

			case R.id.btn_regist:
				registPost();
				break;

			default:
				break;
		}
	}



	private void getCodePost() {
		String type = "1";
		phone = mEditTextPhone.getText().toString().trim();
		if(TextUtils.isEmpty(phone)){
			ToastUtil.showToast(this, "请输入您的手机号");
			return;
		}

		if (!phone.matches(Constants.PHONE_ZZ)) {
			ToastUtil.showToast(this, "请输入正确的手机号码");
			return;
		}

		OKHttpUtils.postAsync(
				this,
				HttpAddress.SERVICE_REGISTER_CODE,
				new RequestParams(this).getCodeParams(phone), this,
				true, 1);
	}
	
	/**
	 * 注册请求
	 */
	private void registPost() {
		phone = mEditTextPhone.getText().toString().trim();
		String code = mEditTextCode.getText().toString().trim();
		String pwd = mEditTextPwd.getText().toString().trim();
		String comPwd = mEditTextComPwd.getText().toString().trim();
		if(TextUtils.isEmpty(phone)){
			ToastUtil.showToast(this, "请输入您的手机号");
			return;
		}
		
		if (!phone.matches(Constants.PHONE_ZZ)) {
			ToastUtil.showToast(this, "请输入有效的手机号");
			return;
		}
		
		
		
		if(TextUtils.isEmpty(code)){
			ToastUtil.showToast(this, "请输入验证码");
			return;
		}
		
		if(TextUtils.isEmpty(pwd)){
			ToastUtil.showToast(this, "请输入新密码");
			return;
		}
		
		if(TextUtils.isEmpty(comPwd)){
			ToastUtil.showToast(this, "请重复输入新密码");
			return;
		}
		
		if(!pwd.equals(comPwd)){
			ToastUtil.showToast(this, "两次输入密码不一样");
			return;
		}
		
		OKHttpUtils.postAsync(this
				, HttpAddress.FIND_PWD_URL
				, new RequestParams(this).getForgetPswParams(phone, pwd, code,3), this
				,true,2);
	}

	@Override
	public void onSuccess(String response, int flag) {
		String status = JsonUtil.getString(response, "status", "");
		switch (flag) {
		case 1:
			
			if("1".equals(status)){
//				ToastUtils.getInstance().makeText(this, "yes");
				countdown();
			}else{
				String msg = JsonUtil.getString(response, "msg", "");
				ToastUtil.showToast(this, msg);
			}
			break;
			
		case 2:
			if("1".equals(status)){
				finish();
			}
			String msg = JsonUtil.getString(response, "msg", "");
			ToastUtil.showToast(this, msg);
			
			break;

		default:
			break;
		}
		
	}

	@Override
	public void onFailure(Request request, IOException e, int flag) {
		
	}
	
	/**
	 * 获取验证码
	 */
	private void countdown() {
		TranslateAnimation amin = new TranslateAnimation(mEditTextPhone.getWidth(),
				mEditTextPhone.getWidth() + 10, mEditTextPhone.getHeight(),
				mEditTextPhone.getHeight());
		if (TextUtils.isEmpty(phone)) {
			ToastUtil.showToast(this, "空了");
			amin.setInterpolator(new CycleInterpolator(2f));
			amin.setDuration(300);
			amin.start();
			mEditTextPhone.setAnimation(amin);
			return;
		}
		TimerCount timer = new TimerCount(60000, 1000, mTextViewCode);
		timer.start();
	}


}
